package com.useeinfo.asg.modules.logging.dao;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.entity.FrontUser;
import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository()
public class FrontUserDao implements CrudDao<FrontUser> {

	private final static Logger logger = LoggerFactory.getLogger(FrontUserDao.class);

	@PersistenceContext
	private EntityManager entityManager;


	// ******************************************************************************
	// ******************************* 代码自动生成开始 *******************************
	// ******************************************************************************

	public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {

		Map<String, Object> conditionHash = new HashMap<>();

		conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);

		if (queryHash == null || queryHash.size() == 0) {
			return conditionHash;
		}

		for (String queryKey : queryHash.keySet()) {
			String queryValue = queryHash.get(queryKey);

			switch (queryKey) {
				case "userName"://标签id
					conditionHash.put("s.userName = ?{paramIndex} ", CommUtil.null2String(queryValue));
					continue;
				case "password"://标签id
					conditionHash.put("s.password = ?{paramIndex} ", CommUtil.null2String(queryValue));
					continue;
			}
		}

		return conditionHash;
	}

	@Override
	public Long totalRecord(Map<String, String> queryHash) {

		Map<String, Object> conditions = getSearchCondition(queryHash);
		TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from FrontUser s ", conditions, entityManager, Long.class);
		return typedQuery.getSingleResult();
	}

	@Override
	public List<FrontUser> findList(QueryParam queryParam) {

		String sqlInfo = queryParam.joinJPQL("select s from FrontUser s ", "order by s.id desc ");
		String orderParam = queryParam.getSqlOrder();
		if(!CommUtil.null2String(orderParam).equals("")) {
			sqlInfo = queryParam.joinJPQL("select s from FrontUser s ", "order by s." + CommUtil.null2String(orderParam));
		}

		Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
		TypedQuery<FrontUser> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, FrontUser.class);

		return queryParam.findPageList(typedQuery);
	}

	@Override
	public FrontUser findModel(Long id) {
		return entityManager.find(FrontUser.class, id);
	}

	@Override
	public Integer add(FrontUser frontUser) {
		entityManager.persist(frontUser);
		logger.info("FrontUserDaoImpl添加FrontUser成功！");
		return 1;
	}

	@Override
	public Integer update(FrontUser frontUser) {

		entityManager.merge(frontUser);
		return 1;
	}


	@Override
	public Integer delete(Long id) {
		FrontUser frontUser = entityManager.find(FrontUser.class, id);
		entityManager.remove(frontUser);
		return 1;
	}

	@Override
	public Integer delete_logic(FrontUser model) {
		FrontUser frontUser = entityManager.find(FrontUser.class, model.getId());
		frontUser.setDeleteStatus(true);
		return  1;
	}

	// ******************************************************************************
	// ******************************* 代码自动生成结束 *******************************
	// ******************************************************************************

}